<?php
class Frames_Controller_Plugin_Page extends  Frames_Controller_Plugin_Base
{
	
	public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request )
	{
		$page = false;
		$url = (string)ltrim(urldecode($request->getPathInfo()),'/');
		
		if(strpos($url,'/') !== false)
		{
			$urlArr = explode('/', $url);
			$url = $urlArr[0];
			$request->setParam($urlArr[1], $urlArr[2]);
		}
		$_view = new Zend_View( );
		$_view->disableJcarousel = false;
		$_view->lang = 'he';
		$_view->addHelperPath("Frames/View/Helper", "Frames_View_Helper");
		$_headMeta =  $_view->headMeta( );
		$_headMeta->appendHttpEquiv("Content-Type","text/html; charset=UTF-8");
		$_table = new Application_Model_DbTable_Pages( );
		$pages = $_table->fetchAll()->toArray();
		$menu = array();
		foreach ($pages as $pageMenu){
			$menu[ ] = array(
				'label' => $pageMenu[ 'url' ],
				'uri' => $pageMenu[ 'url' ],
			);
		}
		$cotaent = new Zend_Navigation($menu);
		$_view->navigation()->setContainer( $cotaent );
		if($url){
			$_adapter = $_table->getAdapter();
			$url = $_adapter->quote($url);
			$page = $_table->fetchRow('url = '.$url );
			$_view->dbProfiler = $_adapter->getProfiler( );
			if($page && ($metaTags = $page->findDependentRowset( 'Application_Model_DbTable_MetaTags' ))){
				$metaTags = $metaTags->toArray( );
				foreach ($metaTags as $metaTag){
					$_headMeta->appendName($metaTag[ 'name' ], $metaTag[ 'contant' ]);
				}
			}
		}
		if(!$page){
			$page = new stdClass( );
			$page->action = $page->controller = 'index';
			$page->title = $_view->translate( 'מסגרות שעשוע' );
			$page->id = -1;
		}
		$_view->headTitle( $page->title );
	
		
		Zend_Controller_Action_HelperBroker::getStaticHelper( 'ViewRenderer' )->setView( $_view );
		$request->setActionName($page->action)
	          	->setControllerName($page->controller)
				->setParam('pageId', $page->id);
	}
}